package com.zeji.androidadvance.activity.touchFollow;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.zeji.androidadvance.R;
import com.zeji.androidadvance.utils.CommonUtil;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.disposables.Disposable;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@SuppressLint("NonConstantResourceId")
public class TouchFollowActivity extends AppCompatActivity {
    @BindView(R.id.touchFollowPlayer)
    View player;
    @BindView(R.id.touchFollowContainer)
    ViewGroup gameContainer;

    private int score = 0;
    private Disposable gameDisposable;
    private List<AutoDropView> scoreItems;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_touch_follow);
        ButterKnife.bind(this);

        setTitle("跟我走！");

        initGame();

        startGame();
    }

    private void initGame() {
        if (scoreItems != null) {
            scoreItems.clear();
        }
        gameContainer.removeAllViews();
        scoreItems = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            AutoDropView autoDropView = new AutoDropView(this);
            ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(SizeUtils.dp2px(10), SizeUtils.dp2px(10));
            layoutParams.startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
            layoutParams.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
            layoutParams.bottomToTop = ConstraintLayout.LayoutParams.PARENT_ID;
            autoDropView.setBackgroundColor(ContextCompat.getColor(this, R.color.purple_500));
            layoutParams.horizontalBias = new Random().nextFloat();
            autoDropView.setEventListener(new AutoDropView.IEventListener() {
                @Override
                public void onAttachTarget() {
                    getScore();
                }

                @Override
                public void onFailed() {
                    endGame();
                }
            });
            autoDropView.setTargetView(player);
            gameContainer.addView(autoDropView, layoutParams);
            scoreItems.add(autoDropView);
        }
    }

    private void endGame() {
        CommonUtil.dispose(gameDisposable);
        for (AutoDropView item : scoreItems) {
            item.end();
        }
        ToastUtils.showLong("你输了！！ 得分：" + score);
    }

    private void startGame() {
        gameDisposable = Observable.interval(125, TimeUnit.MILLISECONDS)
                .filter(aLong -> new Random().nextFloat() * 100 > 25)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(aLong -> {
                    double speed = Math.sqrt(((double) aLong) / 20);
                    // 这里控制速度递增公式，按你想的方法来设置难度
                    scoreItems.get((int) (aLong % 20)).start(speed + 1);
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CommonUtil.dispose(gameDisposable);
        gameContainer.removeAllViews();
        scoreItems.clear();
    }

    private void getScore() {
        score++;
        setTitle("得分：" + score);
    }
}
